<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Systemadmin_model extends CI_Model {
	
	function __construct(){
        parent::__construct();
    }
    
	function add($data = array()){
		$this->db->insert('rsh_system_admin_user', $data);
		return $this->db->insert_id();
	}

	function update($id,$data = array()){
		$this->db->where('id', $id);
		$this->db->update('rsh_system_admin_user', $data);
		return $this->db->affected_rows();
	}
	 /**
	 * Publish system set
	 *
	 * @access	public
	 * @param   array
	 * @return  string
	 */
	function type_add($data = array()){
		$this->db->insert('rsh_system_admin_type', $data);
		return $this->db->insert_id();
	}
	
	 /**
	 * Update system set
	 *	 
     * @param  boolean
	 * @param  array
	 * @return string
	 */
	function type_update($id, $data = array()){
		$this->db->where('id', $id);
		$this->db->update('rsh_system_admin_type', $data);
		return $this->db->affected_rows();
	}

	/**
	* Check system user
	*
	* @access	public
	* @return	bool
	*/
	function check_systemadmin_user($adminname){
		$query = $this->db->query("select 1 from rsh_system_admin_user where adminname='$adminname'");
		$query->num_rows();
		if ($query->num_rows() > 0){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	* Get system admin type
	*
	* @access	public
	* @return	array
	*/
	function get_systemadmin_type($data){
		$page_size = $data['page_size'];
		$page_start = $data['page_start'];
		
		$sql_result = "select * from rsh_system_admin_type";
		$sql_result .= " limit $page_start, $page_size";
		$query = $this->db->query($sql_result);
		$data['type_list'] = $query->result_array();
		
		$sql_count = "select count(*) as count from rsh_system_admin_type";
		$query = $this->db->query($sql_count);
		$data['type_count'] = $query->row()->count;
		return $data;
	}

	function get_systemadmin_type_select($select){
		$sql_result = "select $select from rsh_system_admin_type";
		$query = $this->db->query($sql_result);
		$data = $query->result_array();
		return $data;
	}

	function getone_systemadmin_type($where){
		$sql_result = "select * from rsh_system_admin_type where $where";
		$query = $this->db->query($sql_result);
		$data = $query->row_array();
		return $data;
	}

	function systemadmin_type_del($id){
		$this->db->where('id', $id);
		$data = $this->db->delete('rsh_system_admin_type');
		return $data;
	}

	function systemadmin_user_del($id){
		$this->db->where('id', $id);
		$data = $this->db->delete('rsh_system_admin_user');
		return $data;
	}

	function getone_systemadmin_user($where){
		$sql_result = "select * from rsh_system_admin_user where $where";
		$query = $this->db->query($sql_result);
		$data = $query->row_array();
		return $data;
	}

	function get_systemadmin_user($data){
		$page_size = $data['page_size'];
		$page_start = $data['page_start'];
		
		$sql_result = "select * from rsh_system_admin_user";
		$sql_result .= " limit $page_start, $page_size";
		$query = $this->db->query($sql_result);
		$data['user_list'] = $query->result_array();
		
		$sql_count = "select count(*) as count from rsh_system_admin_user";
		$query = $this->db->query($sql_count);
		$data['user_count'] = $query->row()->count;
		return $data;
	}
	
	// 获取 管理员在线人数
	function get_online_admincount(){
		$_sql = "SELECT  count(1) as online_count from rsh_system_admin_user where(unix_timestamp() - login_time ) < 1800";
		$query = $this->db->query($_sql);
		return $query->row();
	}

	
	function tongji(){
		// 今日注册会员数
		$_sql_1 = " select count(1) as now_count from rsh_user where 
			add_time >=   unix_timestamp( concat(from_unixtime(unix_timestamp(),'%Y-%m-%d'), ' 00:00:01'))
			and add_time <=   unix_timestamp( concat(from_unixtime(unix_timestamp(),'%Y-%m-%d'), ' 23:59:59'))";
		$query = $this->db->query($_sql_1);
		$data['now_count'] = $query->row()->now_count;
		// 共有注册会员数
		$_sql_2 = " select count(user_id) as count from rsh_user";
		$data['count'] = $this->db->query($_sql_2)->row()->count;
		// 合计交易总额
		$_sql_3 = " select sum(money) as sum_money from rsh_borrow_tender where fstatus = 1 ";
		$data['sum_money']= $this->db->query($_sql_3)->row()->sum_money;
		// 已发布标的标数量
		$_sql_4 = " select count(id) as bor_count from rsh_borrow where (fstatus = 1 or fstatus = 3) ";
		$data['bor_count']= $this->db->query($_sql_4)->row()->bor_count;
		
		return $data;
		
	}
}